#pragma once

#include "nlohmann/json.hpp"
#include <string>
#include <vector>

namespace Protocol {
class AppHeartbeatMsgObject {
private:
    std::size_t tick;
    std::size_t time;
    std::string src;
    struct App {
        std::string name;
        std::string status;
        std::string sver;
        struct Point {
            std::string key;
            std::string val;
            std::size_t time;

            Point(const std::string &key, const std::string &val)
                : key(key), val(val), time(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count()) {}

            friend void to_json(nlohmann::json &j, const Point &p) {
                j = {{"key", p.key}, {"val", p.val}, {"time", p.time}};
            }
        };

        std::vector<Point> points;

        App(const std::string &name, const std::string &status, const std::string &sver, const std::vector<Point> &p)
            : name(name), status(status), sver(sver), points(p) {}

        friend void to_json(nlohmann::json &j, const App &a) {
            j = {{"name", a.name}, {"status", a.status}, {"sver", a.sver}, {"points", a.points}};
        }
    };

    std::vector<App> app;

public:
    AppHeartbeatMsgObject()
        : tick(1),
          time(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count()),
          src("dev_dlt698"),
          app({{"dev_dlt698", "online", "V1.0.0", {App::Point("date", "2025-1-17 09:19:29")}}}) {}
    friend void to_json(nlohmann::json &j, const AppHeartbeatMsgObject &a) {
        j = {{"tick", a.tick}, {"time", a.time}, {"src", a.src}, {"app", a.app}};
    }
};
};  // namespace Protocol